<template>
  <div class="container h-100">
    <div class="d-flex justify-content-center ">
      <el-form class="m-auto">
        <el-form-item label="账号">
          <el-input placeholder="请输入内容" v-model="username" clearable></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input placeholder="请输入内容" v-model="password" clearable show-password></el-input>
        </el-form-item>
        <el-form-item label="验证码">
          <div class="d-flex align-items-center">
            <el-input placeholder="请输入验证码" v-model="code" clearable style="width: 10rem"></el-input>
            <img :src="url" @click="updateCode" alt="验证码" title="点击换一张"
                 class="ms-5 border" style="width: 6rem ;height: 2rem">
          </div>
        </el-form-item>
        <div class="d-flex justify-content-center">
          <el-button type="primary" @click="register" style="width: 6rem">注册</el-button>
        </div>
        <div class="text-secondary">
          已经有账号,去<a :href="USER_LOGIN_URL" class="btn small text-primary text-decoration-underline">登陆</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import {UserApi_get_verify_code} from "@/api/api";
import {userRegister} from "@/api/user";
import {USER_LOGIN_URL} from "@/views/user/index";

export default {
  name: "RegisterComponent",
  data() {
    return {
      username: "",
      password: "",
      code: "",
      url: UserApi_get_verify_code,
      USER_LOGIN_URL,
    }
  },
  methods: {
    async updateCode() {
      let host = process.env.VUE_APP_BASE_URL
      this.url = host + UserApi_get_verify_code + "?t=" + Math.random()
    },
    async register() {
      let data = {
        username: this.username,
        password: this.password,
        code: this.code,
      }
      await userRegister(data)
      this.$message.success("注册成功")
      this.$router.push(USER_LOGIN_URL);
    },
    // async init() {
    //   let url = "/api/hello_api/test_base64_img"
    //   let data = await getJson(url)
    //   this.url = data.data
    // },
  },
  created() {
    this.updateCode()
  }
}
</script>

<style scoped>

</style>
